// utils/date.js

/**
 * 日期时间格式化工具函数
 */

// 格式化时间为本地日期时间格式
export function formatToLocalDateTime(date) {
  if (!date) return "";

  // 如果是字符串日期，转换为Date对象
  if (typeof date === "string") {
    // 尝试解析日期字符串
    date = new Date(date.replace(/-/g, "/"));
  }

  // 如果是时间戳，转换为Date对象
  if (typeof date === "number") {
    date = new Date(date);
  }

  // 检查是否是有效日期
  if (!(date instanceof Date) || isNaN(date)) {
    console.error("Invalid date:", date);
    return "";
  }

  // 格式化年月日
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");

  // 格式化时分秒
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");

  // 返回格式化后的日期时间
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 格式化为日期
export function formatDate(date) {
  if (!date) return "";

  // 处理日期转换
  if (typeof date === "string") {
    date = new Date(date.replace(/-/g, "/"));
  }

  if (typeof date === "number") {
    date = new Date(date);
  }

  if (!(date instanceof Date) || isNaN(date)) {
    return "";
  }

  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");

  return `${year}-${month}-${day}`;
}

// 获取当天开始时间
export function getDayStart(date = new Date()) {
  const result = new Date(date);
  result.setHours(0, 0, 0, 0);
  return result;
}

// 获取当天结束时间
export function getDayEnd(date = new Date()) {
  const result = new Date(date);
  result.setHours(23, 59, 59, 999);
  return result;
}

// 格式化剩余时间
export function formatRemainTime(endTime) {
  const now = new Date().getTime();
  const end = new Date(endTime).getTime();
  const milliseconds = end - now;

  if (milliseconds <= 0) {
    return "已结束";
  }

  // 计算天、小时、分钟和秒
  const days = Math.floor(milliseconds / (1000 * 60 * 60 * 24));
  const hours = Math.floor(
    (milliseconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
  );
  const minutes = Math.floor((milliseconds % (1000 * 60 * 60)) / (1000 * 60));
  const seconds = Math.floor((milliseconds % (1000 * 60)) / 1000);

  // 格式化输出
  let result = "";
  if (days > 0) {
    result += `${days}天`;
  }
  if (hours > 0 || days > 0) {
    result += `${hours}小时`;
  }
  if (minutes > 0 || hours > 0 || days > 0) {
    result += `${minutes}分钟`;
  }
  result += `${seconds}秒`;

  return result;
}

// 获取相对时间描述
export function getTimeAgo(dateTime) {
  if (!dateTime) return "";

  const now = new Date();
  const date = new Date(dateTime);
  const diff = now.getTime() - date.getTime();

  // 计算时间差（毫秒）
  const minute = 60 * 1000;
  const hour = 60 * minute;
  const day = 24 * hour;
  const week = 7 * day;
  const month = 30 * day;
  const year = 365 * day;

  if (diff < minute) {
    return "刚刚";
  } else if (diff < hour) {
    return Math.floor(diff / minute) + "分钟前";
  } else if (diff < day) {
    return Math.floor(diff / hour) + "小时前";
  } else if (diff < week) {
    return Math.floor(diff / day) + "天前";
  } else if (diff < month) {
    return Math.floor(diff / week) + "周前";
  } else if (diff < year) {
    return Math.floor(diff / month) + "个月前";
  } else {
    return Math.floor(diff / year) + "年前";
  }
}

// 添加天数
export function addDays(date, days) {
  const result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

// 计算两个日期之间的天数
export function daysBetween(date1, date2) {
  const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
  const first = new Date(date1);
  const second = new Date(date2);

  // 取整
  return Math.round(Math.abs((first.getTime() - second.getTime()) / oneDay));
}
